package com.aishua.aishuawallet.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by aishua on 2018/8/10.
 */

public class TimeUtils {

    /**
     * 得到几天前的时间
     * @param d
     * @param day
     * @return
     */
    public static Date getDateBefore(Date d, int day){
        Calendar now =Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE,now.get(Calendar.DATE)-day);
        return now.getTime();
    }
    /**
     * String   转 Date;
     * @param str
     * @return
     */

    public static Date StringToDate(String str){

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟
        Date date= null;
        try {
            date = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date;
    }


    public static String DateToString(Date date){

        SimpleDateFormat sdf=new SimpleDateFormat("MM/dd");
        String str=sdf.format(date);
        return str;
    }

    /**
     * 获得当天零时零分零秒
     * 求出来是12点的，相差12个小时
     * @return
     */
    public static  Date initDateByDay(){

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();

    }
}
